// pages/project_member_add/project_member_add.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectId: 0,
    username: '',  // 搜索框的昵称
    userArr: [
      // {id: 10001, username: '张三丰', checked: false},
      // {id: 10002, username: '李无敌', checked: false}
    ],
    page: 0,
    pageSize: 18 // 用于查询
  }, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      projectId: options.projectId
    })
    this.reqAddableUsers();
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.setData({
      page: 0,
      userArr: []
    })
    this.reqAddableUsers();
    wx.stopPullDownRefresh();
  },

  /**
   * 上拉加载更多
   */
  onReachBottom() {
    this.reqAddableUsers();
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '添加项目成员'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '添加项目成员'
    }
  },

  /**
   * 初始化用户数据，默认展示10条
   */
  reqAddableUsers() {

    let param = {
      projectId: this.data.projectId, 
      userId: wx.getStorageSync('userId'),
      pageSize: this.data.pageSize, 
      page: this.data.page
    }
    if (this.data.username.length!=0) {
      param['username'] = this.data.username.trim()
    }

    request.GET('/project/addable/members', param).then(res => {
      // console.log(res)
      if (res.data.resCode==200) {
        let currPage = this.data.page;
        let tempArr = this.data.userArr;
        if (res.data.userArr.length!=0) {
          currPage++;
        }
        tempArr = tempArr.concat(res.data.userArr);

        this.setData({
          page: currPage,
          userArr: tempArr
        })
      } else {
        wx.showToast({
          title: '查询异常',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },

  /**
   * 输入框值变化
   */
  bindSearchInput(e) {
    this.setData({
      username: e.detail.value
    })

    if (this.data.username.trim().length==0) {
      this.clickSearchAddableUsers();
    }
  },

  /**
   * 点击搜索
   */
  clickSearchAddableUsers() {
    this.setData({
      page: 0,
      userArr: []
    })
    this.reqAddableUsers();
  },

  /**
   * 点击行选择用户
   */
  clickChooseUser(e) {
    let isChecked = this.data.userArr[e.currentTarget.dataset.idx]['checked'];
    if (isChecked) {
      isChecked = false;
    } else {
      isChecked = true;
    }
    this.data.userArr[e.currentTarget.dataset.idx]['checked'] = isChecked;
    const arr = this.data.userArr;
    this.setData({
      userArr: arr
    })
  },

  /**
   * 点击添加项目社员
   */
  clickSaveAddMember() {
    // 遍历已选择的用户
    let checkedUser = [];
    for (let i=0; i<this.data.userArr.length; i++) {
      if (this.data.userArr[i].checked) {
        checkedUser.push(this.data.userArr[i].id);
      }
    }

    // 提示
    if (checkedUser.length==0) {
      wx.showToast({
        title: '请选择用户',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    wx.showToast({
      title: '添加中',
      icon: 'loading',
      duration: 30000,
      mask: true,
    })

    // 保存
    const param = {
      projectId: this.data.projectId, 
      checkedUserStr: checkedUser +"", 
      userId: wx.getStorageSync('userId')
    };
    request.GET('/project/saveProjectMembers', param).then(res =>{
      // console.log(res)
      if (res.data.resCode==200) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 1000
        })

        setTimeout(function() {
          wx.navigateBack({
            delta: 1,
          })
        })
      } else {
        wx.showToast({
          title: '添加异常',
          icon: 'error',
          duration: 1500
        })
      }
    })
  },
  
})